<?php

namespace app\admin\service;

use app\admin\service\JigsawpuzzleService;

use app\api\controller\Llava;
use think\Db;

class VllmService
{

    public function AutomaticLabeling($id)
    {

        $resultFilePath = Db::table("zstp_article")->where("id", $id)->field("file_path")->find();
        $filePathStr = $resultFilePath['file_path'];
        $filePathArr = json_decode($filePathStr);
        $res = $this->obtainImageVl($filePathArr, $id);
        $type = $res['type'];
        $data = $res['data'];

        $Llava = new Llava();

        $respnonse = $Llava->onlineQwen($data);

        $Arr = json_decode($respnonse, true);

//        var_dump($Arr);


        $content = $Arr['choices'][0]['message']['content'];

        preg_match('/```json\s*({.*?})\s*```/s', $content, $matches);


        $datArr = json_decode($matches[1], true);



        return $datArr;


    }

    function obtainImageVl($filePathArr, $id)
    {

        $path = config("path");
        $path = $path . "increment-data/文字图表";


        $count = count($filePathArr);

        if ($count == 1) {

            $file = $path . "/" . $filePathArr[0];

            return array("code" => 200, "type" => "1", "data" => $file);

        } else {

            $image1 = $path . "/" . $filePathArr[0];

            $image2 = $path . "/" . end($filePathArr);

            $JigsawpuzzleService = new JigsawpuzzleService();

            $data = [$image1, $image2];

            $imagesPath = $JigsawpuzzleService->index($data, $id);

            return array("code" => 200, "type" => "2", "data" => $imagesPath);

        }

    }

}